package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;

public class LoginPage extends Utils {
    public static String url="http://8.137.19.140:9090/blog_login.html";
    public LoginPage() {
        super(url);
    }
    //检查页面是否正确
    public void checkPageRIght() {
        //检查登录标题是否可见
       driver.findElement(By.cssSelector(" body > div.container-login > div > h3"));

        //检查用户名是否可见
       driver.findElement(By.cssSelector("body > div.container-login > div > div:nth-child(2) > span"));
        //检查密码是否可见
        driver.findElement(By.cssSelector("body > div.container-login > div > div:nth-child(3) > span"));
        //检查用户名输入框是否可见
        if (driver.findElement(By.cssSelector("#username")) != null) {
            System.out.println("用户名输入框可见");
        } else {
            System.out.println("用户名输入框不可见");
        }
        //检查密码输入框是否可见
        if (driver.findElement(By.cssSelector("#password")) != null) {
            System.out.println("密码输入框可见");
        } else {
            System.out.println("密码输入框不可见");
        }
        //检查提交按钮是否可见
        if (driver.findElements(By.cssSelector("#submit")) != null) {
            System.out.println("提交按钮可见");
        } else {
            System.out.println("提交按钮不可见");
        }
        //检查目录主页按钮是否可见
        if (driver.findElements(By.cssSelector("body > div.nav > a:nth-child(4)")) != null) {
            System.out.println("目录可见");
        } else {
            System.out.println("目录不可见");
        }
        //检查目录写博客按钮是否可见
        if (driver.findElements(By.cssSelector("body > div.nav > a:nth-child(5)")) != null) {
            System.out.println("写博客按钮可见");
        } else {
            System.out.println("写博客按钮不可见");
        }
        //检查头像是否可见
        if (driver.findElements(By.cssSelector("body > div.nav > img")) != null) {
            System.out.println("头像可见");
        } else {
            System.out.println("头像不可见");
        }
        //检查昵称是否可见
        if (driver.findElement(By.cssSelector("body > div.nav > span")) != null) {
            System.out.println("昵称可见");
        }else{
            System.out.println("昵称不可见");
        }
    }
    //登录
    public void LoginSuc() throws IOException {
        //先清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
   //输入正确用户名和密码
     driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
     driver.findElement(By.cssSelector("#password")).sendKeys("123456");
    //然后点击提交
        driver.findElement(By.cssSelector("#submit")).click();
        //然后校验登录是否成功
        //只要登录成功，目录栏就会出现注销
        //如果存在就证明登录成功
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        if(driver.findElements(By.cssSelector("body > div.nav > a:nth-child(6)"))!=null){
            System.out.println("登录成功");
        }
    }
    //登录失败
    public void LoginFail() throws IOException {
        //输入错误用户名和密码
        driver.findElement(By.cssSelector("#username")).sendKeys("leizhengyu");
        driver.findElement(By.cssSelector("#password")).sendKeys("111111");

        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //然后点击提交
        driver.findElement(By.cssSelector("#submit")).click();
        //然后校验登录是否失败
        //只要登录失败，弹窗就会出现提示信息就会出现
        //等待弹窗出现
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert= driver.switchTo().alert();


        //获取弹窗内容
        String alertText=alert.getText();
        System.out.println("弹窗内容："+alertText);

        //点击确认
        alert.accept();
        }

    }

